package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.InventoryLocationMapper;
import com.kang.pojo.InventoryLocation;
import com.kang.service.InventoryLocationService;
import com.vonechina.entity.page.InventoryLocationPage;

/**
 * 库位服务层实现类
 * 
 * @author zs614
 *
 */
@Service
public class InventoryLocationServiceImpl implements InventoryLocationService {
	/**
	 * 库位持久层
	 */
	@Resource
	private InventoryLocationMapper itl;

	/**
	 * 分页查询库位
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<InventoryLocation> listInventoryLocation(InventoryLocationPage page) {
		page.setRows(itl.countInventoryLocation(page));
		List<InventoryLocation> list = itl.listInventoryLocation(page);
		return list;
	}

	/**
	 * 删除下级库位
	 * 
	 * @param inventorylocationid
	 *            库位ID
	 * @return
	 */
	@Override
	public int deletenext(Integer inventorylocationid) {

		return itl.deleteChild(inventorylocationid);
	}

	/**
	 * 查询下级库位
	 * 
	 * @param inventorylocationid
	 *            库位ID
	 * @return
	 */
	@Override
	public List<InventoryLocation> listInventoryLocationById(Integer inventorylocationid) {

		return itl.listInventoryLocationById(inventorylocationid);
	}

	/**
	 * 删除库位
	 * 
	 * @param inventorylocationid
	 *            库位ID
	 * @return
	 */
	@Override
	public int deleteByPrimaryKey(Integer inventorylocationid) {

		return itl.deleteByPrimaryKey(inventorylocationid);
	}

	/**
	 * 新增库位
	 * 
	 * @param record
	 *            库位实体类
	 * @return
	 */
	@Override
	public int insertSelective(InventoryLocation record) {

		return itl.insertSelective(record);
	}

	/**
	 * 通过主键查询库位
	 * 
	 * @param inventorylocationid
	 *            库位ID
	 * @return
	 */
	@Override
	public InventoryLocation selectByPrimaryKey(Integer inventorylocationid) {

		return itl.selectByPrimaryKey(inventorylocationid);
	}

	/**
	 * 更新库位
	 * 
	 * @param record
	 *            库位实体类
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(InventoryLocation record) {

		return itl.updateByPrimaryKeySelective(record);
	}

	/**
	 * 生成库位编号
	 * 
	 * @return
	 */
	@Override
	public String generatoCode() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = itl.selectMaxCode();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		return "KW" + no;
	}

}
